/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import api.CollectionManager;
import api.DatabaseManager;
import api.Statistics;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.basex.core.BaseXException;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author peto
 */
public class TeamComparison extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");

        
        if (request.getServletPath().equals("/statistics/team-comparsion")) {
             twoTeamsCompasion(request, response);
        }else {
            throw new RuntimeException("Unknown operation: " + request.getServletPath());
        }
    }
    
    // --------------------------------------------------------------------------------------
    
    private void twoTeamsCompasion(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException{
        
        DatabaseManager dm = new DatabaseManager();
        Statistics stats = new Statistics(dm);
        String lid = request.getParameter("lid");
        String team1 = request.getParameter("t1");
        String team2 = request.getParameter("t2");
        Locale locale = request.getLocale();
        ResourceBundle labelsBundle = ResourceBundle.getBundle("labelsBundle", locale);
        
        if(lid == null || team1 == null || team2 ==null){
         //  request.setAttribute("error", labelsBundle.getString("parameters_not_set"));
         //  request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
        request.setAttribute("lid", lid);
       // team1 = "Slovan Bratislava";
       // team2 = "Spartak Trnava";
       // lid = "corgon-liga-2009-2010";
        
        CollectionManager cm = new CollectionManager(lid, dm);
        try {
            request.setAttribute("xml_teams", cm.getTeamList());
            request.setAttribute("xml_menu", cm.getLeagueList());
            if(team1 != null && team2 != null){
                request.setAttribute("xml", stats.getMatchesByTeams(lid, team1, team2));
                request.setAttribute("charts", stats.getTeamsProgress(lid));
                request.setAttribute("team1", team1);
                request.setAttribute("team2", team2);
            }
        } catch (BaseXException ex) {
            Logger.getLogger(TeamComparison.class.getName()).log(Level.SEVERE, null, ex);
             request.setAttribute("xml", ex.getMessage());
        }
        request.getRequestDispatcher("/teamsComparison.jsp").forward(request, response);
    }
    
    
    // --------------------------------------------------------------------------------------

    
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
